package cn.ryh.rabbitmq.work.fair.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;

/**
 * 公平队列
 * 需要在yml文件加入
 *  spring.rabbitmq.listener.ssimple.acknowledge-mode: MANUAL
 *  spring.rabbitmq.listener.ssimple. prefetch: 1
 * @author Raoyh
 * @Description
 * @create 2022-07-14 9:07
 */
// @Component
@Slf4j
public class FairWorkConsumer {
    //每个@RabbitListener都会启动一个新的消费者
    // @RabbitListener(bindings = @QueueBinding(//这里进行绑定设置
    //         value = @Queue,//队列,默认属性，随机命名，非持久，独占，排他。自动删除
    //         exchange = @Exchange(name = "logs",declare = "false")///指定logs交换机,不创建交换机，因为主程序已经定义，只引用已经存在的交换机
    // ))
    // @RabbitListener(queues = "email.fairWork.queue")
    public void receive(String message, Channel channel,
                        @Header(name = AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {
        log.info("receive - 收到: " + message);
        Thread.sleep(200);
        // 手动发送确认回执
        channel.basicAck(tag, false);
    }

    // @RabbitListener(queues = "email.fairWork.queue")
    public void receive2(String message, Channel channel,
                         @Header(name = AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {
        log.info("receive2 - 收到: " + message);
        Thread.sleep(1000);
        // 手动发送确认回执
        channel.basicAck(tag, false);
    }
}
